import { createApp } from 'vue'
import { createRouter, createWebHistory } from 'vue-router'
import WelcomePage from '@/components/welcomeView/WelcomePage'
import PatientPage from '@/components/patient/PatientPage'
import DoctorPage from '@/components/doctor/DoctorPage'
import PatientMessagePage from '@/components/admin/pages/MessagePage.vue'
import AdminPage from '@/components/admin/AdminPage';
import { ElMessage } from 'element-plus';
import store from '@/store';

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/',
      name: 'Welcome',
      component: WelcomePage
    },
    {
      path: '/patient',
      name: 'Patient',
      component: PatientPage,
      meta: { requiresAuth: true },
      children: [
        {
          path: 'appointment',
          name: 'Appointment',
          component: () => import('@/components/patient/pages/AppointmentPage'),
          meta: { requiresAuth: true }
        },
        {
          path: 'profile',
          name: 'profile',
          component: () => import('@/components/patient/pages/ProfilePage'),
          meta: { requiresAuth: true }
        },
        {
          path: 'settings',
          name: 'Settings',
          component: () => import('@/components/patient/pages/SettingsPage'),
          meta: { requiresAuth: true }
        },
        {
          path: 'medical-record',
          name: 'MedicalRecord',
          component: () => import('@/components/patient/pages/MedicalRecordPage'),
          meta: { requiresAuth: true }
        },
        {
          path: 'comment',
          name: 'Comment',
          component: () => import('@/components/patient/pages/CommentPage'),
          meta: { requiresAuth: true }
        },
        {
          path: 'payment',
          name: 'Payment',
          component: () => import('@/components/patient/pages/PaymentPage'),
          meta: { requiresAuth: true }
        },
        {
          path: 'patientnotice',
          name: 'Patientnotice',
          component: () => import('@/components/patient/pages/NoticePage'),
          meta: { requiresAuth: true }
        },
        {
          path: 'patientmessage',
          name: 'PatientMessage',
          component: () => import('@/components/patient/pages/MessagePage'),
          meta: { requiresAuth: true }
        }
      ]
    },
    {
      path: '/doctor',
      name: 'Doctor',
      component: DoctorPage,
      meta: { requiresAuth: true },
      children: [
        {
          path: 'doctorNotification',
          name: 'DoctorNotification',
          component: () => import('@/components/doctor/pages/NotificationPage'),
          meta: { requiresAuth: true },
        },
        {
          path: 'book',
          name: 'Book',
          component: () => import('@/components/doctor/pages/BookPage'),
          meta: { requiresAuth: true },
        },
        {
          path: 'bookcal',
          name: 'Bookcal',
          component: () => import('@/components/doctor/pages/BookCalPage'),
          meta: { requiresAuth: true },
        },
        {
          path: 'patientManage',
          name: 'PatientManage',
          component: () => import('@/components/doctor/pages/PatientManagePage'),
          meta: { requiresAuth: true },
        },
        {
          path: 'diagnosis',
          name: 'Diagnosis',
          component: () => import('@/components/doctor/pages/DiagnosisPage'),
          meta: { requiresAuth: true },
        },
        {
          path: 'doctorMessage',
          name: 'DoctorMessage',
          component: () => import('@/components/doctor/pages/MessagePage'),
          meta: { requiresAuth: true },
        },
        {
          path: 'judgement',
          name: 'Judgement',
          component: () => import('@/components/doctor/pages/JudgementPage'),
          meta: {requiresAuth: true},
        },
        {
          path: 'bill',
          name: "Bill",
          component: () => import('@/components/doctor/pages/BillPage'),
          meta: {requiresAuth: true},
        }
      ]
    },
    {
      path: '/admin',
      name: 'Admin',
      component: () => import('@/components/admin/AdminPage'),
      meta: { requiresAuth: true },
      children: [
        {
          path:'statistic',
          name: 'Statistic',
          component: () => import('@/components/admin/pages/StatisticPage'),
          meta: { requiresAuth: true }
        },
        {
          path:'schedule',
          name: 'Schedule',
          component: () => import('@/components/admin/pages/SchedulePage'),
          meta: { requiresAuth: true }
        },
        {
          path:'home',
          name: 'Home',
          component: () => import('@/components/admin/pages/HomePage'),
          meta: { requiresAuth: true }
        },
        {
          path:'message',
          name: 'Message',
          component: () => import('@/components/admin/pages/MessagePage'),
          meta: { requiresAuth: true }
        },
        {
          path:'notice',
          name: 'Notice',
          component: () => import('@/components/admin/pages/NoticePage'),
          meta: { requiresAuth: true }
        }
      ]
    },
    {
      path:'/admin',
      redirect: '/admin/statistic'
    },
  ],
})


router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    if (!store.getters.userIsLoggedIn) {
      ElMessage.warning('请先登录');
      next('/');
    } else {
      const userType = store.getters.userType;
      console.log('userType:', '/' + userType);
      if (!to.path.startsWith('/' + userType)) { // 这个认证很弱智 建议改了
          ElMessage.warning('您没有权限访问该页面');
          next('/');
      } else {
          next();
      }
    }
  } else {
    next();
  }
});



export default router;
